<template>
    <section>
        <el-row>
            <el-col>
                <el-form :inline="true" :model="filter" class="demo-form-inline">
                    <el-form-item label="输入搜索">
                        <el-input v-model="filter.key" placeholder="群组id"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="search">查询</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table :data="list" @selection-change="">
                    <el-table-column prop="avatar" label="头像" width="120">
                        <template slot-scope="scope">
                            <img :src="scope.row.avatar" alt="头像" class="avatar">
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="群组名称" width="120">
                    </el-table-column>
                    <el-table-column prop="id" label="群组id" width="120">
                    </el-table-column>
                    <el-table-column prop="desc" label="群组介绍" width="120">
                    </el-table-column>
                    <el-table-column prop="address" label="群组地址" width="120">
                    </el-table-column>
                    <el-table-column prop="aaaa" label="操作" min-width="120">
                        <template slot-scope="scope">
                            <el-button @click="detail(scope.row)">查看群组成员</el-button>
                            <el-button @click="del(scope.row)">删除小组</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <div :span="24" style="text-align: center;margin-top: 20px">
            <el-pagination
                background
                layout="prev, pager, next"
                :total="total"
                :page-size="size"
                :current-page.sync="currentPage"
                @current-change="Init">
            </el-pagination>
        </div>

        <el-dialog
            title="详细信息"
            :visible.sync="dialogVisible"
            width="400">
            <el-table :data="current">
                <el-table-column prop="name" label="头像" width="100">
                    <template slot-scope="scope">
                        <img :src="scope.row.user_info.avatar" alt="" class="avatar">
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="Id" width="100">
                </el-table-column>
                <el-table-column prop="name" label="姓名" width="100">
                    <template slot-scope="scope">
                        {{scope.row.user_info.name}}
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="介绍" min-width="120">
                    <template slot-scope="scope">
                        {{scope.row.user_info.Introduction}}
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="地区" width="120">
                    <template slot-scope="scope">
                        {{scope.row.user_info.detail_address}}
                    </template>
                </el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </section>
</template>
<script>
    /* eslint-disable indent,space-before-function-paren,spaced-comment */
    import {getGroupList, deleteGroup} from '../../api/api'

    export default {
        data() {
            return {
                filter: {
                    key: ''
                },
                list: [],
                total: 0,
                size: 5,
                dialogVisible: false,
                current: {},
                currentPage: 0
            }
        },
        methods: {
            Init(page = 1) {
                var para = {
                    page: page,
                    size: this.size
                }
                if (this.filter.key) {
                    para.key = this.filter.key
                }
                console.log(para.page)
                getGroupList(para).then(
                    data => {
                        if (data.status) {
                            this.list = data.data.map(x => {
                                return {
                                    id: x.group_id,
                                    name: x.name,
                                    desc: x.Introduction || '',
                                    address: x.detail_address || '',
                                    avatar: x.avatar || '',
                                    member: x.group_member || {}
                                }
                            })
                            if (page === 1) {
                                this.total = data.total
                                this.currentPage = 1
                            }
                        } else {
                            this.$message.error(data.msg)
                        }
                    }
                ).catch()
            },
            search() {
                this.Init()
            },
            detail(a) {
                this.current = a.member
                this.dialogVisible = true
            },
            del(b) {
                this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteGroup({
                        id: b.id
                    }).then(
                        data => {
                            if (data.status) {
                                this.$message.success('删除成功')
                                this.Init()
                            } else {
                                this.$message.error(data.msg)
                            }
                        }
                    ).catch()
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            }
        },
        mounted() {
            this.Init()
        }
    }
</script>
<style scoped>
    section {
        /*background: red;*/
    }

    .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
    }

    .wallpaper {
        width: 100%;
        border-radius: 8px;
    }
</style>
